//
//  GYSLaunchPageView.m
//  AiHome
//
//  Created by macbook on 2018/1/27.
//  Copyright © 2018年 yguan. All rights reserved.
//

#import "GYSLaunchPageView.h"
#import "GYSCircleSkipButton.h"

// 倒计时时间
static int const showtime = 3;

@interface GYSLaunchPageView ()

// 启动页图
@property (nonatomic,strong) UIImageView *imageView;

// 跳过按钮
@property (nonatomic, strong) GYSCircleSkipButton *circleSkipButton;

@end

@implementation GYSLaunchPageView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // 1.启动页图片
        _imageView = [[UIImageView alloc]initWithFrame:frame];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageView.image = [UIImage imageNamed:@"AppIcon"];
        [self addSubview:_imageView];
        // 2.跳过按钮
        GYSCircleSkipButton *circleSkipButton = [[GYSCircleSkipButton alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 55, 30, 40, 40)];
        circleSkipButton.lineWidth = 2;
        [circleSkipButton setTitle:@"跳过" forState:UIControlStateNormal];
        [circleSkipButton setTitleColor:[UIColor  colorWithRed:197/255.0 green:159/255.0 blue:82/255.0 alpha:1] forState:UIControlStateNormal];
        circleSkipButton.titleLabel.font = [UIFont systemFontOfSize:14];
        
        [circleSkipButton addTarget:self action:@selector(removeProgress) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:circleSkipButton];
        self.circleSkipButton = circleSkipButton;
        
    }
    return self;
}

- (void)show {
    //  progress 完成时候的回调
    __weak __typeof(self) weakSelf = self;
    [weakSelf.circleSkipButton startAnimationDuration:showtime withBlock:^{
        [weakSelf removeProgress];
    }];
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
}

// 移除启动页面
- (void)removeProgress {
    self.imageView.transform = CGAffineTransformMakeScale(1, 1);
    self.imageView.alpha = 1;
    [UIView animateWithDuration:0.3 animations:^{
        self.circleSkipButton.hidden = NO;
        self.imageView.alpha = 0.05;
        self.imageView.transform = CGAffineTransformMakeScale(5, 5);
    } completion:^(BOOL finished) {
        self.circleSkipButton.hidden = YES;
        [self.imageView removeFromSuperview];
    }];
}

@end
